package jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQPrefetchPolicy;
import org.apache.activemq.command.ActiveMQQueue;

public class PtpConsumerSyn {
	
	
	public void custome() throws JMSException{
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		ActiveMQPrefetchPolicy p = new ActiveMQPrefetchPolicy();
		p.setQueuePrefetch(1);
		activeMQConnectionFactory.setPrefetchPolicy(p);
		ConnectionFactory factory = activeMQConnectionFactory;
		Connection conn = factory.createConnection();
		Destination queue =  new ActiveMQQueue("allen");
		conn.start();
		Session session = conn.createSession(false,Session.CLIENT_ACKNOWLEDGE);
		MessageConsumer consumer = session.createConsumer(queue);
		TextMessage message = (TextMessage) consumer.receive();
		System.out.println(message.getText());
//		int m = 1/0;
//		TextMessage message2 = (TextMessage) consumer.receive();
//		System.out.println(message2.getText());
		try {
			if (message.getText().equals("hello2")) {
				Thread.sleep(30000);
			}else{
				Thread.sleep(1000);
			}
			message.acknowledge();
			System.out.println("sack"+message.getText()+System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//		message.acknowledge();
		session.close();
		conn.close();
	}
	public void custome2() throws JMSException{
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		ActiveMQPrefetchPolicy p = new ActiveMQPrefetchPolicy();
		p.setQueuePrefetch(1);
		activeMQConnectionFactory.setPrefetchPolicy(p);
		ConnectionFactory factory = activeMQConnectionFactory;
		Connection conn = factory.createConnection();
		Destination queue =  new ActiveMQQueue("allen");
		conn.start();
		Session session = conn.createSession(false,Session.CLIENT_ACKNOWLEDGE);
		MessageConsumer consumer = session.createConsumer(queue);
//		while(true){
			TextMessage message = (TextMessage) consumer.receive();
			System.out.println(message.getText());
			try {
				if (message.getText().equals("hello2")) {
					Thread.sleep(30000);
				}else{
					Thread.sleep(1000);
				}
				message.acknowledge();
				System.out.println("ack"+message.getText()+":"+System.currentTimeMillis());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
//		}
		
		session.close();
		conn.close();
	}
	
	public static void main(String[] args) throws JMSException, InterruptedException {
		new Thread(new Runnable() {
			
			public void run() {
				try {
					new PtpConsumerSyn().custome();
				} catch (JMSException e) {
					e.printStackTrace();
				}
				
			}
		}).start();
		new Thread(new Runnable() {
					
					public void run() {
						try {
							new PtpConsumerSyn().custome2();
						} catch (JMSException e) {
							e.printStackTrace();
						}
						
					}
				}).start();
		
	}
}
